package cn.maxhou.crm.controller;

import cn.maxhou.crm.entity.User;
import cn.maxhou.crm.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Wrapper;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private ISysUserService userService;

   @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request) {
        User user=userService.login(usrName, usrPassword);
        if (user!=null) {
            request.getSession().setAttribute("loginUser", user);
            return "main";
        }else {
            request.setAttribute("message", "登录失败,用户名或密码错误!");
            return "login";
        }
    }

    @RequestMapping(value = "logout")
    public String logout(HttpSession session){
       session.removeAttribute("loginUser");
       return "login";
    }
    @RequestMapping(value = "/user/list")
    public String findUsers(String usrName, Long roleId, Model model){
        Page<User> userPage=userService.findByPage(usrName, roleId, 1, 5);
        model.addAttribute("userPager", userPage);
        return "/user/list";
    }
}
